$().ready(function() {
	validateRule();
    $("#groupAdd").on("click",function(){
        $("#groupTab tbody").append('<tr align="center"><td height="45">' +
            '<input type="text" name="group_name" class="form-control" data-parsley-required="true"></td>' +
            '<td width="2"></td><td><button type="button" class="btn btn-default"><i class="fa fa-plus mr5"></i>管理参数</button></td>'+
            '<td><input type="text" name="group_params" class="form-control" style="width: 200px;" readonly></td>'+
            '<td><i class="up fa fa-arrow-up" style="cursor:pointer;padding: 5px;" title="向上移动"></i>' +
            '<i class="down fa fa-arrow-down" style="cursor:pointer;padding: 5px;" title="向下移动"></i>' +
            '<i class="del fa fa-remove" style="cursor:pointer;padding: 5px;" title="删除"></i></td></tr>');
        binGroupEvent();
    });
    $("#dialogParamAdd").on("click",function(){
        $("#dialogParamTab tbody").append('<tr align="center"><td height="45">' +
            '<input type="text" class="form-control"></td>' +
            '<td width="2"></td>' +
            '<td style="width: 80px;"><i class="up fa fa-arrow-up" style="cursor:pointer;padding: 5px;" title="向上移动"></i>' +
            '<i class="down fa fa-arrow-down" style="cursor:pointer;padding: 5px;" title="向下移动"></i>' +
            '<i class="del fa fa-remove" style="cursor:pointer;padding: 5px;" title="删除"></i></td></tr>');
        binDialogGroupEvent();
    });
    $("#specAdd").on("click",function(){
        layer.open({
            type:2,
            title:"选择规格",
            area : [ '300px', '450px' ],
            content:"/wx/goodsType/spec"
        });
    });
});

$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
function update() {


	$.ajax({
		cache : true,
		type : "POST",
		url : "/wx/goodsType/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入名字"
			}
		}
	})
}

function binDialogGroupEvent(){
    $("#dialogParamTab .up").unbind("click").on("click",function(){
        var p=$(this).parent().parent();
        var nextTR = p.prev();
        if (nextTR.length > 0) {
            nextTR.insertAfter(p);
        }
    });
    $("#dialogParamTab .down").unbind("click").on("click",function(){
        var p=$(this).parent().parent();
        var nextTR = p.next();
        if (nextTR.length > 0) {
            nextTR.insertBefore(p);
        }
    });
    $("#dialogParamTab .del").unbind("click").on("click",function(){
        var tr=$("#dialogParamTab tbody").find("tr");
        if (tr.length > 0) {
            $(this).parent().parent().remove();
        }
    });
}
function loadSpec(str) {
    $("#specTab tbody").append(str);
    binSpecEvent();
}